package com.zjitc.serviceedu.controller;


import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduChapter;
import com.zjitc.serviceedu.entity.chapter.ChapterVo;
import com.zjitc.serviceedu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/serviceedu/edu-chapter")
@Api(tags = "章节模块")
public class EduChapterController {
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @Autowired
    private EduChapterService chapterService;

    @GetMapping("getChapterList/{courseId}")
    @ApiOperation("课程大纲")
    public RespBean getChapterList(@PathVariable Long courseId){
      List<ChapterVo> list =  chapterService.getChapterAndChapterTwo(courseId);
        return RespBean.success().data("list",list).msg("成功");
    }

    @ApiOperation("添加章节")
    @PostMapping("addChapter")
    public RespBean addChapter(EduChapter eduChapter){
        boolean save = chapterService.save(eduChapter);
        if(!save) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }

    @ApiOperation("查询章节详情")
    @GetMapping("getChapterInfo/{ChapterId}")
    public RespBean getChapterInfo(@PathVariable Long ChapterId){
        EduChapter eduChapter = chapterService.getById(ChapterId);
        return RespBean.success().data("data",eduChapter).msg("查询成功");
    }

    @ApiOperation("修改章节")
    @PutMapping("updateChapter")
    public RespBean updateChapter(EduChapter eduChapter){
        boolean rows = chapterService.updateById(eduChapter);
        if(!rows) {
            throw new AuthException("更新失败");
        }

        return RespBean.success().msg("更新成功");
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/chapters/{chapterId}")
    public RespBean deleteChapter(@PathVariable Long chapterId){
        boolean rows = chapterService.removeById(chapterId);
        if (!rows){
            throw new AuthException("删除失败");
        }
        return RespBean.success().data("rws",rows).msg("删除成功");
    }
}


